﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;
using G.WeiBo.WebAPI.Domain;
using G.WeiBo.WebAPI.Response;
using G.WeiBo.WebAPI.Request;
using G.WeiBo.WebAPI;

namespace G.WeiBo.View
{
    public partial class TopicBrowserPage : GBasePage
    {
        DefaultClient client = GlobalStaticData.Client;
        public TopicBrowserPage()
        {
            InitializeComponent();
        }
        string topicName = string.Empty;
        protected override void OnOrientationChanged(OrientationChangedEventArgs e)
        {
            base.OnOrientationChanged(e);
            switch (e.Orientation)
            {
                case PageOrientation.LandscapeLeft:
                case PageOrientation.LandscapeRight:
                    txtTopicName.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                case PageOrientation.PortraitUp:
                    txtTopicName.Visibility = System.Windows.Visibility.Visible;
                    break;
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            listBoxMessage.EnableTombstoneListBox<Message>(this);
            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.New)
            {
                if (this.NavigationContext.QueryString.ContainsKey("topicName"))
                {
                    topicName = this.NavigationContext.QueryString["topicName"];
                    txtTopicName.Text = topicName;
                    GetTopics();
                }
            }
        }

        private void GetTopics()
        {
            txtTopicName.Text = topicName;
            listBoxMessage.BeginLoadingData();
            client.Execute(new GetTopicMessagesRequest { TopicName = topicName }, (c) =>
            {
                listBoxMessage.LoadData<Message>(c.Response);
            });
        }


        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (topicName == string.Empty)
            {
                MessageBox.Show("请输入您想要关注的话题");
                return;
            }
            this.listBoxMessage.ClearItems();
            GetTopics();
        }

        private void btnAddTopic_Click(object sender, EventArgs e)
        {
            if (topicName == string.Empty)
            {
                MessageBox.Show("请输入您想要关注的话题");
                return;
            }
            client.Execute(new FollowTopicRequest { TopicName = topicName }, (c) =>
            {
                if (!c.IsError)
                {
                    this.ShowState("关注成功");
                }
            });
        }

        private void WatermarkTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox t = sender as TextBox;
            if (e.Key == Key.Enter)
            {
                this.Focus();
                if (t.Text != string.Empty)
                {
                    topicName = t.Text;
                    this.listBoxMessage.ClearItems();
                    GetTopics();
                }
            }

        }

        private void listBoxMessage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBoxMessage.SelectedItem != null)
            {
                this.NavigationService.Navigate("/View/CommentsList.xaml", listBoxMessage.SelectedItem);
                listBoxMessage.SelectedItem = null;
            }
        }
    }
}